<template>
	<div>
		<MemberRule></MemberRule>
	</div>
</template>

<script>
import MemberRule from './tstorememberrule.vue';
export default {
	components: {
		MemberRule
	},
	data() {
		return {
			memberTypeList: [],
			visible: false,
			dataForm: {
				id: 0,
				sellerId: '',
				memberTypeId: '',
				exchangeAmount: '',
				exchangeIntegral: '',
				integralClearRuleStatus: '',
				integralClearRule: '',
				integralClearDate: '',
				minQuota: '',
				upgradeRule: '',
				startIntegral: '',
				endIntegral: ''
			},
			dataRule: {
				sellerId: [{ required: true, message: '租户不能为空', trigger: 'blur' }],
				memberTypeId: [{ required: true, message: '会员类型不能为空', trigger: 'blur' }],
				exchangeAmount: [{ required: true, message: '兑换金额(消费多少金额兑换多少积分)不能为空', trigger: 'blur' }],
				exchangeIntegral: [{ required: true, message: '兑换积分(消费多少金额兑换多少积分)不能为空', trigger: 'blur' }],
				integralClearRuleStatus: [{ required: true, message: '积分清零状态(当为1的时候清零积分)不能为空', trigger: 'blur' }],
				integralClearRule: [{ required: true, message: '积分清零规则不能为空', trigger: 'blur' }],
				integralClearDate: [{ required: true, message: '积分清零日期不能为空', trigger: 'blur' }],
				minQuota: [{ required: true, message: '最低限额不能为空', trigger: 'blur' }],
				upgradeRule: [{ required: true, message: '会员升级规则(1按历史积分升级2按当前积分)不能为空', trigger: 'blur' }],
				startIntegral: [{ required: true, message: '开始积分(积分区间可打折)不能为空', trigger: 'blur' }],
				endIntegral: [{ required: true, message: '结束积分(积分区间可打折)不能为空', trigger: 'blur' }]
			}
		};
	},
	methods: {
		init(id) {
			//会员类型列表
			this.$http({
				url: this.$http.adornUrl('/store/tstoremembertype/list'),
				method: 'get',
				params: this.$http.adornParams({
					status: 1
				})
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.memberTypeList = data.page.list;
				} else {
					this.memberTypeList = [];
				}
			});

			this.dataForm.id = id || 0;
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields();
				if (this.dataForm.id) {
					this.$http({
						url: this.$http.adornUrl(`/member/tstorememberrule/info/${this.dataForm.id}`),
						method: 'get',
						params: this.$http.adornParams()
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.dataForm.sellerId = data.tStoreMemberRule.sellerId;
							this.dataForm.memberTypeId = data.tStoreMemberRule.memberTypeId;
							this.dataForm.exchangeAmount = data.tStoreMemberRule.exchangeAmount;
							this.dataForm.exchangeIntegral = data.tStoreMemberRule.exchangeIntegral;
							this.dataForm.integralClearRuleStatus = data.tStoreMemberRule.integralClearRuleStatus;
							this.dataForm.integralClearRule = data.tStoreMemberRule.integralClearRule;
							this.dataForm.integralClearDate = data.tStoreMemberRule.integralClearDate;
							this.dataForm.minQuota = data.tStoreMemberRule.minQuota;
							this.dataForm.upgradeRule = data.tStoreMemberRule.upgradeRule;
							this.dataForm.startIntegral = data.tStoreMemberRule.startIntegral;
							this.dataForm.endIntegral = data.tStoreMemberRule.endIntegral;
						}
					});
				}
			});
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate(valid => {
				if (valid) {
					this.$http({
						url: this.$http.adornUrl(`/member/tstorememberrule/${!this.dataForm.id ? 'save' : 'update'}`),
						method: 'post',
						data: this.$http.adornData({
							id: this.dataForm.id || undefined,
							sellerId: this.dataForm.sellerId,
							memberTypeId: this.dataForm.memberTypeId,
							exchangeAmount: this.dataForm.exchangeAmount,
							exchangeIntegral: this.dataForm.exchangeIntegral,
							integralClearRuleStatus: this.dataForm.integralClearRuleStatus,
							integralClearRule: this.dataForm.integralClearRule,
							integralClearDate: this.dataForm.integralClearDate,
							minQuota: this.dataForm.minQuota,
							upgradeRule: this.dataForm.upgradeRule,
							startIntegral: this.dataForm.startIntegral,
							endIntegral: this.dataForm.endIntegral
						})
					}).then(({ data }) => {
						if (data && data.code === 0) {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.visible = false;
									this.$emit('refreshDataList');
								}
							});
						} else {
							this.$message.error(data.msg);
						}
					});
				}
			});
		}
	}
};
</script>

<style></style>
